<?php
class Product_Model_Receiving
{

	private $_table;
	//Initalizing Db Table  
	public function __construct(){
		$this->_table = new Product_Model_DbTable_Receiving();
	}
	
	/**
	*add DB Adapter
	*/
	public function getAdapter(){
		return $this->_table->getAdapter();
	}
	
	public function add($row){
		return $this->_table->insert($row);
	}

	
	public function getByReceivingCode($code){
		$where = $this->_table->getAdapter()->quoteInto('receiving_code= ?', $code);
		return $this->_table->fetchRow($where);
	}
	
    public function getByCondition($condition=array(),$type='*',$page="",$pageSize="",$orderBy=''){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
//         $select->where(" order_type=?",1);	//值为1  收货类型为 标准
        if(isset($condition["order_type"]) && $condition['order_type'] != ""){
        	$select->where("order_type=?",$condition['order_type']);
        }
        if(isset($condition["order_types"]) && $condition['order_types'] != ""){
        	$select->where("order_type in(?)",$condition['order_types']);
        }
		if(isset($condition["receiving_code"]) && $condition['receiving_code'] != ""){
			$select->where("receiving_code=?",$condition['receiving_code']);
		}
		if(isset($condition["status"]) && $condition['status'] !== ""){
			$select->where("status=?",$condition['status']);
		}
		if(isset($condition["ref_id"]) && $condition['ref_id'] != ""){
			$select->where("ref_id like ?",'%'.$condition['ref_id'].'%');
		}
		if(isset($condition["customer_id"]) && $condition['customer_id'] != ""){
			$select->where("customer_id=?",$condition['customer_id']);
		}
		
		if(isset($condition["receiving_codes"]) && $condition['receiving_codes'] != ""){
			$select->where("receiving_code in(?)",$condition['receiving_codes']);
		}
// 		print_r($select->__toString());exit;
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
			$select->order($orderBy);
			if(isset($condition['sortColumn']) && $condition['sortColumn'] != ""){
				$select->order($condition['sortColumn']." ".$condition['sortMethod']);
			}
            if($pageSize>0 and $page>0)
            {
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	/**
	 * 获取 RMA 退货信息
	 *
	 * @param 
	 * @return
	 */
	public function getReturnsByCondition($condition=array(),$type='*',$page="",$pageSize="",$orderBy=array()){
        $select = $this->_table->getAdapter()->select();
        $table = $this->_table->info('name');
        $select->from($table,$type);
        $select->where("1 =?",1);
        $select->where(" order_type >?",1);
		if(isset($condition["receiving_code"]) && $condition['receiving_code'] != ""){
			$select->where("receiving_code=?",$condition['receiving_code']);
		}
		if(isset($condition["status"]) && $condition['status'] !== ""){
			$select->where("status=?",$condition['status']);
		}
		if(isset($condition["ref_id"]) && $condition['ref_id'] != ""){
			$select->where("ref_id=?",$condition['ref_id']);
		}
		if(isset($condition["customer_id"]) && $condition['customer_id'] != ""){
			$select->where("customer_id=?",$condition['customer_id']);
		}
        if('count(*)'==$type){
            return $this->_table->getAdapter()->fetchOne($select);
        }else{
			$select->order($orderBy);
            if($pageSize>0 and $page>0){
                $start =($page-1)*$pageSize;
                $select->limit($pageSize,$start);
            }
			$sql=$select->__toString();
		    return $this->_table->getAdapter()->fetchAll($sql);
        }
	}
	
	public function update($row,$receivingCode){
		$where = $this->_table->getAdapter()->quoteInto("receiving_code = ?",$receivingCode);
		return $this->_table->update($row,$where);
	}
	
	
	public function getByValue($value,$field='receiving_id'){
        $select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'*');
        $select->where("{$field} = ?",$value);
		return $this->_table->getAdapter()->fetchRow($select);
	}
	
	/**
	 * 获取当天最后一个 退货code
	 */
	public function getCurdateLastReturnCode($customer_id){
		$select = $this->_table->getAdapter()->select();
        $table  = $this->_table->info('name');
        $select->from($table,'max(receiving_code)');
        $mindate = date('Y-m-d').' 00:00:00';
        $maxdate = date('Y-m-d').' 23:59:59';

        $select->where("created > ?",$mindate);
        $select->where("created < ?",$maxdate);
        $select->where("order_type > ?",1);
        $select->where("customer_id = ?",$customer_id); 
		return $this->_table->getAdapter()->fetchOne($select);
	}
	
}